package mlz.c_map;

import java.util.Comparator;
import java.util.TreeMap;

//排序
public class TreeMapDemo06 {
    public static void main(String[] args) {
        //自然排序
        TreeMap<Integer,String> map = new TreeMap<>();
        map.put(6,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(1,"赵六");
        map.put(4,"张三");
        System.out.println(map);


        //比较器排序
        TreeMap<Person, String> persons = new TreeMap<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();//升序排序
            }
        });

        persons.put(new Person("张三",18),"杭州");
        persons.put(new Person("李四",12),"上海");
        persons.put(new Person("王五",19),"北京");
        persons.put(new Person("赵六",20),"深圳");
        persons.put(new Person("孙七",30),"杭州");

        System.out.println(persons);
    }
}
